﻿using Snake.GameBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;


namespace Snake.Data
{
    class BarrierOperater
    {
        static JavaScriptSerializer js = new JavaScriptSerializer();

        public static string GetJson(Barrier barrier)
        {
            var result = js.Serialize(barrier);
            return result;
        }

        public static Scene GetSceneFromJson(string json,System.Drawing.Graphics g)
        {
            var barrier = js.Deserialize<Barrier>(json);
            GameComponentFactory.ColorOfBackground = barrier.FactoryOptions.BackGround.GetColor();
            GameComponentFactory.ColorOfFood = new System.Drawing.SolidBrush(barrier.FactoryOptions.Food.GetColor());
            GameComponentFactory.ColorOfSnakeBody = new System.Drawing.SolidBrush(barrier.FactoryOptions.SnakeBody.GetColor());
            GameComponentFactory.ColorOfSnakeHead = new System.Drawing.SolidBrush(barrier.FactoryOptions.SnakeHead.GetColor());
            GameComponentFactory.ColorOfWall = new System.Drawing.SolidBrush(barrier.FactoryOptions.Wall.GetColor());
            GameComponentFactory.ColorOfGate = new System.Drawing.SolidBrush(barrier.FactoryOptions.Gate.GetColor());
            GameComponentFactory.ColorOfTrap = new System.Drawing.SolidBrush(barrier.FactoryOptions.Trap.GetColor());
            var scene = new Scene(barrier.SceneSize.Width, barrier.SceneSize.Height);
            var snake = new Snake(scene.FreePoints)
            {
                Head = GameComponentFactory.CreateSnakeHead(new System.Drawing.Point(barrier.Snake.Head.X, barrier.Snake.Head.Y))
            };       
            foreach(var p in barrier.Snake.Bodys)
            {
                snake.Components.Add(GameComponentFactory.CreateSnakeBody(new System.Drawing.Point(p.X, p.Y)));
            }
            snake.Direction = Snake.SnakeDirection.None;
            scene.Snake = snake;
            var foodmgr = new FoodManager(scene.FreePoints)
            {
                MaxFoodNum = barrier.FoodManager.MaxFoodNum
            };
            scene.FoodManager = foodmgr;
            
            var wallmgr = new WallManager(scene.FreePoints);
            foreach(var p in barrier.WallManager.Walls)
            {
                wallmgr.Components.Add(GameComponentFactory.CreateWall(new System.Drawing.Point(p.X, p.Y)));
            }
            scene.WallManager = wallmgr;

            GateManager gateManager = new GateManager();
            foreach(var gatedata in barrier.GatesD.Gates)
            {
                gateManager.Gates.Add(GameComponentFactory.CreateGate(new System.Drawing.Point(gatedata.Location.X, gatedata.Location.Y), new System.Drawing.Point(gatedata.Arrival.X, gatedata.Arrival.Y),gatedata.Direction));
            }
            scene.GateManager = gateManager;

            TrapManager trapmanager = new TrapManager(scene.FreePoints)
            {
                Traps = new List<IGameComponent>()
            };
            foreach(var trapdata in barrier.TrapManger.Traps)
            {
                trapmanager.Traps.Add(GameComponentFactory.CreateTrap(new System.Drawing.Point(trapdata.X, trapdata.Y)));
            }
            scene.TrapManager = trapmanager;

            scene.Score = 0;
            scene.Speed = barrier.StartSpeed;
            scene.Graphics = g;
            foreach(var wall in scene.WallManager.Components)
            {
                scene.FreePoints.Remove(wall.Position);
            }
            foreach(var gate in scene.GateManager.Gates)
            {
                scene.FreePoints.Remove(gate.Position);
            }
            foreach (var trap in scene.TrapManager.Traps)
            {
                scene.FreePoints.Remove(trap.Position);
            }
            return scene;
        }
    }
}
